<?php
require_once("C_colorCalc.inc");

class ColorGraph extends ColorCalc {
	private $_useColorPercents;
	private $_showPercents;
	private $_showTotals;
	private $_textClass;

	public function __construct() {
		ColorCalc::__construct();
		$this->_useColorPercents=true;
		$this->_showPercents=true;
		$this->_showTotals=true;
		$this->_textClass='';
	}
	
	public function getHorrizBar($count, $total) {
		# Calculate percent
		$pct = !$total 
			? 0.00 
			: floor(($count*100)/$total);
		
		#Check for and correct >100%
		ColorCalc::cleanPctRange($pct);
		
		# Make colors for percent and graph
		$pctColor = "rgb(" . ColorCalc::getRGBCalc($pct) . ")";
		$progressBox = "
			<div style=\"border:solid black 1px;background-color:#fff;\" width=\"100px\" >
				<div style=\"height:12px;border:solid $pctColor 1px; background-color:$pctColor;width:$pct%;font-weight:bold;color:white;\"></span>
			<div>
		";
		
		if( $this->_showPercents ) {
			$colorPercents = 
				$this->_useColorPercents
				? "color:$pctColor;"
				: "";
				
			$dispPct = "<td align=\"right\"><span class=\"{$this->_textClass}\"><span style=\"$colorPercents\">$pct</span>%</span></td>";
		}
		
		if( $this->_showTotals ) {
			$dispTotals = "<td align=\"left\"><span class=\"{$this->_textClass}\">($count/$total)</span></td>";
		}

		return "<table border=\"0\" cellpadding=\"2\" cellspacing=\"0\">
			 <tr>
			  $dispPct
			  <td width=\"150\" align=\"left\">$progressBox</td>
			  $dispTotals
			 </tr>
			</table>
		";
	}
	
	// Set class to use for display text
	public function setTextClass($class)  { 
		$this->_textClass = $class; 
	}
	
	// Show the percentages
	public function showPercents()  { 
		$this->_showPercents = true; 
	}
	// Hide the percentages
	public function hidePercents() { 
		$this->_showPercents = false; 
	}
	
	// Show the totals
	public function showTotals()  { 
		$this->_showTotals = true; 
	}
	// Hide the totals
	public function hideTotals() { 
		$this->_showTotals = false; 
	}

	// Color the percentages
	public function colorPercentsOn()  { 
		$this->_useColorPercents = true; 
	}
	// Don't color the percentages
	public function colorPercentsOff() { 
		$this->_useColorPercents = false; 
	}
}
